package net.zjitc.dao.impl;

import net.zjitc.dao.SecondDao;
import net.zjitc.domain.CategorySecond;
import net.zjitc.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 2 * @Author: 洪瑶
 * 3 * @Date: 2018/10/30 8:17
 * 4
 */
public class SecondDaoImpl implements SecondDao{
    @Override
    public List<CategorySecond> sqlfindAll() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from categorysecond";
        List<CategorySecond> list = (List<CategorySecond>) queryRunner.query(sql,new BeanListHandler(CategorySecond.class));
        return list;
    }

    @Override
    public boolean sqladd(CategorySecond second) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "insert into categorysecond (csname)values(?)";
        int num = queryRunner.update(sql,new Object[]{second.getCsname()});
        if ( num > 0 )
        {
            return true;
        }
        return false;
    }

    @Override
    public void sqldelete(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "delete from categorysecond where csid = ?";
        queryRunner.update(sql,id);
    }

    @Override
    public CategorySecond sqlfindBy(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from categorysecond where csid = ?";
        CategorySecond second = queryRunner.query(sql, new BeanHandler<CategorySecond>(CategorySecond.class),id);
        return second;
    }

    @Override
    public boolean sqlUpdate(CategorySecond second, String id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "update categorysecond set csname = ? where csid = ?";
        int num = queryRunner.update(sql,new Object[]{second.getCsname(),id});
        if (num > 0 )
        {
            return true;
        }
        return false;
    }
}
